package com.ssy.lingxi.member.merchant.serviceimpl.feign;

import com.ssy.lingxi.common.constant.member.MemberTypeEnum;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.service.feign.IProductFeignService;
import com.ssy.lingxi.product.api.feign.ICommodityFeign;
import com.ssy.lingxi.product.api.model.request.CommodityCreditScore;
import com.ssy.lingxi.product.api.model.request.feign.OffPublishCommodityRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 调用商品服务Feign接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-01-24
 */
@Service
public class ProductFeignServiceImpl implements IProductFeignService {
    private static final Logger logger = LoggerFactory.getLogger(ProductFeignServiceImpl.class);

    @Resource
    private ICommodityFeign commodityFeign;

    /**
     * 平台会员被冻结时，通知商品服务下架所有商品
     *
     * @param memberId   会员Id
     * @param roleId     角色Id
     * @param memberTypeEnum 会员类型枚举
     */
    @Async
    @Override
    public void asyncNotifyToOffProducts(Long memberId, Long roleId, Integer memberTypeEnum) {
        try {
            OffPublishCommodityRequest request = new OffPublishCommodityRequest();
            request.setMemberId(memberId);
            request.setMemberRoleId(roleId);
            if (memberTypeEnum.equals(MemberTypeEnum.MERCHANT.getCode()) || memberTypeEnum.equals(MemberTypeEnum.MERCHANT_PERSONAL.getCode())) {
                request.setIsMember(true);
            } else {
                request.setIsMember(false);
            }

            Wrapper<Boolean> result = commodityFeign.offPublishAllCommodity(request);

            if(result == null) {
                logger.error("会员被冻结时，异步通知商品服务下架会员商品错误, 返回结果为null");
                return;
            }

            if(result.getCode() != ResponseCode.SUCCESS.getCode() || !result.getData()) {
                logger.error("会员被冻结时，异步通知商品服务下架会员商品错误, code:" + result.getCode() + ", data:" + result.getData());
            }

        } catch (Exception e) {
            logger.error("会员被冻结时，异步通知商品服务下架会员商品错误", e);
        }
    }

    /**
     * 更新商品对应的店铺平台信用积分
     * @param memberId        平台会员Id
     * @param roleId     平台会员角色Id
     * @param memberTypeEnum 会员类型枚举
     * @param plusCreditScore 平台会员信用积分变动的分数
     */
    @Override
    public void updateCommodityCreditScoreAsync(Long memberId, Long roleId, Integer memberTypeEnum, Integer plusCreditScore) {
        try {
            CommodityCreditScore creditScore = new CommodityCreditScore();
            creditScore.setMemberId(memberId);
            creditScore.setMemberRoleId(roleId);
            if(memberTypeEnum.equals(MemberTypeEnum.MERCHANT.getCode()) || memberTypeEnum.equals(MemberTypeEnum.MERCHANT_PERSONAL.getCode())) {
                creditScore.setIsChannelMember(false);
            } else {
                creditScore.setIsChannelMember(true);
            }
            creditScore.setCreditScore(plusCreditScore);
            commodityFeign.updateCommodityCreditScore(creditScore);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("更新商品对应的店铺积分错误,MemberId:" + memberId + ", roleId:" + roleId, e);
        }
    }
}
